<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>商品分类管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
    
</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">商品分类管理     
                       	</h3>
                    </div>

                    <div class="box-body">
                  			 <ol class="breadcrumb" id="olId">
                        		<li id="0">
		                        	<a onclick="getData(0)" >顶级分类列表</a>
		                        </li>
	                        </ol>

                        <!-- 数据表格 -->
                        <div class="table-box">
							
                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
										<button type="button"  class="btn btn-default" title="删除" onclick="deleteBatch()"><i class="fa fa-trash-o"></i> 删除</button>
                                        <button type="button" class="btn btn-default" title="刷新" ><i class="fa fa-check"></i> 刷新</button>
                                       
                                    </div>
                                </div>
                            </div>                          

			                <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">分类ID</th>
									      <th class="sorting">分类名称</th>									   
									      <th class="sorting">类型模板ID</th>
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="itemCatList">

			                      </tbody>
			                  </table>
							<!--<div th:replace="common/common :: foot"></div>-->
			                  <!--数据列表/-->
                        </div>
                        <!-- 数据表格 /-->
                     </div>
                    <!-- /.box-body -->
              
                                
<!-- 编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">商品分类编辑</h3>
		</div>
		<div class="modal-body">

			<input  type="hidden" id="parentId" >
			<table class="table table-bordered table-striped"  width="800px">
				<tr>
		      		<td>上级商品分类</td>
		      		<td >
		      		   <div id="ol"></div>
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>商品分类名称</td>
		      		<td><input  id="itemCat" class="form-control" placeholder="商品分类名称">  </td>
		      	</tr>			  
		      	<tr>
		      		<td>类型模板</td>
		      		<td>
						<select id="tempselct2" style="width: 100%">
							<option th:each="template : ${template}" th:value="${template.id}" >[[${template.name}]]</option>
						</select>
		      		</td>		      		      		
		      	</tr>		      	
			 </table>				
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" onclick="save()" data-dismiss="modal" aria-hidden="true">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>

  <!-- 修改窗口 -->
  <div class="modal fade" id="editModal2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabel2">商品分类编辑</h3>
			  </div>
			  <div class="modal-body">

				  <table class="table table-bordered table-striped" id="itemCatUpdateInfo"  width="800px">
					  <tr>
						  <td>上级商品分类</td>
						  <td>
							  <div id="ol2"></div>
						  </td>
					  </tr>
					  <tr>
						  <input type="hidden" id="itemId">
						  <td>商品分类名称</td>
						  <td><input  id="itemName" class="form-control" placeholder="商品分类名称">  </td>
					  </tr>
					  <tr>
						  <td>类型模板</td>
						  <td>
							  <select id="tempselct" style="width: 100%">
								  <option th:each="template : ${template}" th:value="${template.id}" >[[${template.name}]]</option>
							  </select>
						  </td>
					  </tr>
				  </table>

			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success"  onclick="update()" data-dismiss="modal" aria-hidden="true">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>

  <!--查询-->
<script>
	$(function () {
		getData(0);
	})
	//定义一个接收变量的数组
	var dhlList = [];
	//展示页面拼接
	function getData(id,i){
		//定义变量接受数据
		var itemCarName = dhlList[id,i];
		//从新定义数组
		dhlList = [];

		$.ajax({
			url:"/itemCatController/getItemCatInfo",
			type:"get",
			data:{parentId:id},
			dataType:"json",
			async:false,
			success:function(page){
				var result=page.data;
				var info = "";
				if (result.length <= 0){
					alert("没有下一级");
					return;
				}
				$(result).each(function (i,e) {
					info+= '<tr>';
					info+= '<td><input  type="checkbox" value="'+e["id"]+'"></td>';
					info+= '<td>'+e['id']+'</td>';
					info+= '<td>'+e['name']+'</td>';
					info+= '<td>'+e['typeId']+'</td>';
					info+= '<td class="text-center">';
					info+= '<button type="button" onclick="getData('+e["id"]+','+i+')" class="btn bg-olive btn-xs" >查询下级</button><button type="button" onclick="updateInfo('+e["id"]+')" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal2" >修改</button>';
					info+= '</td>';
					info+= '</tr>';
					dhlList.push(e.name)
				})
				//拼接导航
				var $info = $("<li id='"+id+"'><a href='javascript:getData("+id+",-2)'>"+itemCarName+"</a></li>");
					$info.appendTo($("#olId"));
				//根据腹肌id删除
				$("#"+id+"").nextAll().remove()
				$("#itemCatList").html(info);

				$("#parentId").val(id);
				var olInfos = $("#olId").find("li").find("a");
				var olText="";
				$(olInfos).each(function (i,e) {
					olText+=e.text
					if (i != olInfos.length-1){
						olText+=">>"
					}
				})
				$("#ol").html(olText)
				$("#ol2").html(olText)
			},
			error:function(page){
				alert(page.message);
			}
		})
	}
</script>

<!--修改-->
<script>
	//根据id调取后台数据进行修改回显
	function updateInfo(id) {
		$.ajax({
			url: "/itemCatController/updateInfo",
			dataType: "json",
			data:{id:id},
			type: "post",
			success: function (result) {
				console.log(result);
				if (result.code == 200) {
					var itemCat = result.data;
					$("#itemId").val(itemCat.id);
					$("#itemName").val(itemCat.name);
					$("#tempselct").val(itemCat.typeId).trigger("change");
					console.log(table);
				} else {
					alert(result.message)
				}
			},
			error: function () {
				toastr.error("系统错误，请联系管理员");
			}
		})
	}
	//修改
	function update(){
		//id
		var id = $("#itemId").val();
		//商品分类名称
		var name = $("#itemName").val();
		var typeId = $("#tempselct").val();

		$.ajax({
			url:"/itemCatController/update",
			type:"post",
			data:{id:id,name:name,typeId:typeId},
			dataType:"json",
			async:false,
			success:function(result){
				if (result.code == 200) {
					alert(result.message);
					window.location.reload();
				}
			},
			error:function(result){
				alert(result.message);
			}
		})
	}
</script>
  <!--增加-->
  <script>
	  //select2的取值方式
	  function save() {
		  //父级id
		  var parentId = $("#parentId").val();
		  //类型名称
		  var name = $("#itemCat").val();
		  //模板类型id
		  var typeId = $("#tempselct2").val();
		  console.log(typeId)

		  $.ajax({
			  url:"/itemCatController/insertItemCat",
			  type:"post",
			  data:{parentId:parentId,name:name,typeId:typeId},
			  dataType:"json",
			  async:false,
			  success:function(result){
				  if (result.code == 200) {
					  alert(result.message);
					  window.location.reload();
				  }
			  },
			  error:function(result){
				  alert(result.message);
			  }
		  })
	  }
  </script>

  <script>
	  //批量删除
	  function deleteBatch() {
		  var ids = [];
		  var boxs = $("[type=checkbox]:checked");
		  for (var i = 0; i < boxs.length; i++) {
			  ids.push(boxs[i].value);
		  }
		  alert(ids);
		  if(ids.length <= 0){
			  alert("请选择需要删除的数据");
			  return;
		  }
		  if (confirm("是否确认删除?")){
			  $.ajax({
				  url:"/itemCatController/deleteBatch",
				  type:"post",
				  data:{ids:ids},
				  dataType:"json",
				  async:true,
				  success:function (result) {
					  if (result.code == 200){
						  alert(result.message);
						  window.location.reload();
					  } else {
						  toastr.error("删除失败");
					  }
				  },
				  error:function(result){
					  alert(result.message);
				  }
			  })
		  }
	  }
  </script>


</body>
</html>